<?php
namespace jy_weishop;
defined('BY_JYA') or exit('error');
/*
	订单
*/
class game extends \plugin{
	public function add_game($params){
		$new_game_wait=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$params['mid'],
			'game_id'=>$params['game_id'],
			'status'=>0,
			'createtime'=>TIMESTAMP,
			'remark'=>$params['remark'],
		);
		$this->pdo_insert('game_wait',$new_game_wait);
		$id=$this->pdo_insertid();
		$setting=$this->plugin_setting('setting');
		$news=array(
			'title'=>'恭喜，您获得活动参与资格',
			'description'=>"快来参加吧，礼品多多哦！",
			'url'=>custom_url('jy_weishop/game.give//id='.$id,true),
			'picurl'=>$setting['share_image'],
		);
		model('core/account')->send_news($news,$params['mid']);
		return true;
	}
	//检查游戏权限
	public function check_game($id=0,$gameid=0){
		if($id){
			$wait=$this->pdo_get('game_wait',array('status'=>0,'id'=>$id,'mid'=>$_SESSION['uid']));
		}else{
			$wait=$this->pdo_get('game_wait',array('status'=>0,'game_id'=>$gameid,'mid'=>$_SESSION['uid']));
		}
		if(!$wait['game_id']){
				exi('你暂无参与该游戏的名额！','error');
		}
		if($wait['game_id']){
			$wait['game_id']=pdo_getcolumn('jy_weishop_game',array('id'=>$wait['game_id']),'game_id');
			return true;
		}elseif($wait['plugin_name'] && $wait['plugin_name']!='core'){
			$gameinfo=$this->plugin($wait['plugin_name'])->get_active($id);
			return $gameinfo;
		}
		return false;
	}
	//发放奖品
	public function get_gameres($id=0,$game_id=0){
		if($id){
			$wait=$this->pdo_get('game_wait',array('id'=>$id,'mid'=>$_SESSION['uid'],'status'=>0));
			$game=pdo_get('jy_weishop_game',array('id'=>$wait['game_id'],'uniacid'=>$_SESSION['uniacid']));
		}elseif($game_id){
			$game=pdo_get('jy_weishop_game',array('id'=>$game_id,'uniacid'=>$_SESSION['uniacid']));
			$wait=$this->pdo_get('game_wait',array('game_id'=>$game_id,'mid'=>$_SESSION['uid'],'status'=>0));
		}
		if($game['type']!=4 && (!$wait || !$game['id'])){
			exi('你已参与或游戏不存在！','error','jy_weishop/index/');
		}
		if($game['type']==4){
			$isfeelog=$this->pdo_get('game_getlog',array('game_id'=>$game['id'],'pin_id'=>0,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
			if($isfeelog['id']){
				exi('你已参与该游戏！','error','jy_weishop/index/');
			}
		}
		if($game['plugin_name'] && $game['plugin_name']!='jy_h5game' && $game['plugin_name']!='jy_weishop'){
			$game['pluginset']=json_decode($game['pluginset'],true);
			$fee=$this->get_fee($game['id']);
			$status=$game['pluginset']['get_type']!=1?1:0;
			$feelog=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$_SESSION['uid'],
					'game_id'=>$game['id'],
					'rewardtype'=>$game['pluginset']['rewardtype'],
					'fee'=>$fee,
					'status'=>$status,
					'createtime'=>TIMESTAMP
			);
			if($status){				
				$this->pdo_insert('game_getlog',$feelog);
			}else{
				$this->pdo_insert('game_feelog',$feelog);
				if($this->session('pin_id')){
					$feelog['pin_id']=$this->session('pin_id');
					$feelog['fee']=$this->get_sharefee($game['id'],$feelog['pin_id']);
					$this->pdo_insert('game_feelog',$feelog);
				}
			}			
		}
		if($wait['id']){
			$this->pdo_update('game_wait',array('status'=>1),array('id'=>$wait['id']));
		}
		$return=array('error'=>0,'fee'=>$fee);
		return $return;
	}
	//获取参与奖励
	public function get_fee($gameid=0){
		$game=pdo_get('jy_weishop_game',array('id'=>$gameid,'uniacid'=>$_SESSION['uniacid']));
		$game['pluginset']=json_decode($game['pluginset'],true);
		if($game['pluginset']){
			$red_max=$game['pluginset']['red_max']?:0;
			$red_min=$game['pluginset']['red_min']?:0;
			$fee=mt_rand($red_min*100,$red_max*100);
			$fee=$fee/100;
		}
		return $fee;
	}
	//获取拉新奖励
	public function get_sharefee($gameid=0,$pin_id=0){
		$game=pdo_get('jy_weishop_game',array('id'=>$gameid,'uniacid'=>$_SESSION['uniacid']));
		$game['pluginset']=json_decode($game['pluginset'],true);
		if($game['pluginset']){
			$feelognum=pdo_count('jy_weishop_game_feelog',array('game_id'=>$gameid,'pin_id'=>$pin_id,'status'=>0,'uniacid'=>$_SESSION['uniacid']));
			$red_shareset=$game['pluginset']['red_shareset']?:array();
			$nownum=0;
			if(count($red_shareset)){
				paixu($red_shareset,'num','desc');
				foreach($red_shareset as $rv){
					if($rv['num']>=$feelognum){
						$red_max=$rv['red_max']?:0;
						$red_min=$rv['red_min']?:0;
						$fee=mt_rand($red_min*100,$red_max*100);
						$fee=$fee/100;
					}
				}
			}			
		}
		return $fee;
	}
}